package com.ai_chat.sys.controller;

import com.ai_chat.common.util.JwtUtil;
import com.ai_chat.common.vo.CodeType;
import com.ai_chat.common.vo.Result;
import com.ai_chat.sys.entity.Topic;
import com.ai_chat.sys.entity.User;
import com.ai_chat.sys.service.impl.TopicServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@RestController
@CrossOrigin
@RequestMapping("/topic")
public class TopicController {

    @Autowired
    private TopicServiceImpl topicService;

    @Autowired
    private JwtUtil jwtUtil;

    @GetMapping("/getTopicList")
    public Result<List<Map<String, Object>>> getTopicList(@RequestHeader("Authorization") String token){
        User user;
        try{
            user = jwtUtil.parseToken(token, User.class);
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail(CodeType.TOKEN_EXPIRED_OR_INVALID);
        }

        List<Topic> topicList = topicService.getTopicList(user.getId());
        List<Map<String, Object>> data = new ArrayList<>();
        for(Topic topic : topicList){
            Map<String, Object> topicMap = new HashMap<>();
            topicMap.put("id", topic.getId());
            topicMap.put("title", topic.getTitle());
            data.add(topicMap);
        }
        return Result.success(data);
    }



    @PostMapping("/createTopic")
    public Result<Map<String,Object>> createTopic(@RequestHeader("Authorization") String token, @RequestBody Topic topic){
        User user;
        try{
            user = jwtUtil.parseToken(token, User.class);
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail(CodeType.TOKEN_EXPIRED_OR_INVALID);
        }

        topic.setUserId(user.getId());

        topicService.createTopic(topic);

        return Result.success();
    }


    @PostMapping("/deleteTopic")
    public Result<Map<String,Object>> deleteTopic(@RequestHeader("Authorization") String token, @RequestBody Topic topic){
        User user;
        try{
            user = jwtUtil.parseToken(token, User.class);
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail(CodeType.TOKEN_EXPIRED_OR_INVALID);
        }

        topicService.deleteTopic(topic);
        return Result.success();
    }

    @PostMapping("/deleteUserTopicList")
    public Result<Map<String,Object>> deleteUserTopicList(@RequestHeader("Authorization") String token,
                                                          @RequestBody Topic topic){
        User user;
        try{
            user = jwtUtil.parseToken(token, User.class);
        }catch (Exception e){
            e.printStackTrace();
            return Result.fail(CodeType.TOKEN_EXPIRED_OR_INVALID);
        }
        topicService.deleteUserTopicList(topic);
        return Result.success();
    }

}
